import { BaseContext } from "koa";
import { getRepository } from "typeorm";
import authorize from "../../middleware/authorize";
import { Get, Post, Controller } from "../../middleware/request";
import { Exception } from "../../utils/exception";
import { Category } from "../entity/Category";
import { auth as Auth } from "../../utils/auth";
import { encrypt, decrypt } from "../../utils/crypto";
@Controller("/api/category")
export class CarouselController {
	@Get("/list")
	async getOrder(ctx: BaseContext) {
		try {
			const classifyRepository = getRepository(Category)
			let classifyList = await classifyRepository.find()
			ctx.body = {
				...Exception(200, "查询成功"),
				data: classifyList,
			}
		} catch (error) {
			console.log(error);

			ctx.body = Exception(112, "查询失败");
		}
	}

	@Post('/add')
	async addCarousel(ctx: BaseContext) {
		try {
			const categoryRepository = getRepository(Category)
			const data = ctx.request.body;
			let category = new Category()
			category = data
			await categoryRepository.save(category)
			ctx.body = Exception(200, "添加成功");
		} catch (error) {
			ctx.body = Exception(112, "添加失败");
		}
	}

	@Post('/del')
	async delOrder(ctx: BaseContext) {
		try {
			const { id } = ctx.request.body;
			if (!id) {
				return ctx.body = Exception(112, "id不能为空")
			}
			const classifyRepository = getRepository(Category)
			const classify = await classifyRepository.findOne(id)
			if (!classify) return ctx.body = Exception(112, "id不存在");
			await classifyRepository.remove(classify)
			ctx.body = Exception(200, "删除成功");
		} catch (error) {
			ctx.body = Exception(112, "删除失败");
		}
	}
}
